package dk.itu.mobile.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class RESTClient implements Clientable {
	
	public static final String URI = "http://trustcare.itu.dk/task-manager-rest/tasks/";

	public String GetAttendantTasks(String AttendantId) {
		String response = "";
		String uri = URI + "attendant/" + AttendantId;
		
		response = DoRestCall(uri, "GET", "");
		
		return response;
	}

	public String CreateTask(String taskXml) {
		String response = "";
		String uri = URI + "createtask";
		
		response = DoRestCall(uri, "POST", taskXml);
		
		return response;
	}

	public String DeleteTask(String taskId) {
		String response = "";
		String uri = URI + "DeleteTask?taskId=" + taskId;
		
		response = DoRestCall(uri, "DELETE", taskId);		
		
		return response;
	}
	
	
    public String DoRestCall(String requestUri, String httpMethod, String postdata) {
        String response = "";
        try {
            // connect to the web service via HTTP GET
            URL url = new URL(requestUri);
            HttpURLConnection connection =
                    (HttpURLConnection) url.openConnection();
               
            
            connection.setRequestMethod(httpMethod);

            if ("POST".equals(httpMethod)) {

                connection.setDoOutput(true);
                
                connection.setRequestProperty("Content-Type", "application/xml"); 

                OutputStreamWriter outputStream =
                        new OutputStreamWriter(connection.getOutputStream());
                
                outputStream.write(postdata);
                
                outputStream.flush();
            }

            connection.connect();

            response = ReceiveResponse(connection);

            connection.disconnect();

        } catch (IOException e) {
            System.err.println(e.toString());
        }
        return response;

    }   
    
    
    private String ReceiveResponse(HttpURLConnection connection) throws IOException {
        String response = "";
        try {
            InputStream inputStream = connection.getInputStream();
            // read the contents
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                for (int i = 0; i < length; i++) {
                    response += (char) buffer[i];
                }
            }
            inputStream.close();
        } catch (IOException e) {
            int responseCode = connection.getResponseCode();
            switch (responseCode) {
            }
        }
        return response;
    }
    
    public static void main(String[] args) {
    	Clientable testClient = new RESTClient();
    	String testAttendant = "rao";
    	String testAttendant2 = "dianaross";
    	String testTaskXML = "<task id='club54' name='The End of Modern-day Gomorrah' date='04-02-1980' status='so-executed'><description>Closing party of Club 54</description><attendants>andywarhole, michaeljackson, dianaross, rao</attendants></task></tasks>";
    	String testTaskId = "club54";
    	
    	//Test get GetAttendantTasks method
    	String attendantsTasks =  testClient.GetAttendantTasks(testAttendant);
    	//Attendants task as xml
    	System.out.println(testAttendant + "'s  tasks: \n" + attendantsTasks);
    	System.out.println();
    	
    	//Test CreateTask method
    	String createTaskResult = testClient.CreateTask(testTaskXML);
    	System.out.println("createTaskResult: " + createTaskResult);
    	System.out.println();
    	
    	//Test on testAttendant2
    	attendantsTasks =  testClient.GetAttendantTasks(testAttendant2);
     	//Attendants task as xml
    	System.out.println(testAttendant2 + "'s  tasks: \n" + attendantsTasks);
    	System.out.println();
    	System.out.println();
    	
    	//Test CreateTask method
    	createTaskResult = testClient.CreateTask(testTaskXML);
    	System.out.println("createTaskResult: " + createTaskResult);
    	System.out.println();
    	
    	//Test on testAttendant2
    	attendantsTasks =  testClient.GetAttendantTasks(testAttendant2);
     	//Attendants task as xml
    	System.out.println(testAttendant2 + "'s  tasks: \n" + attendantsTasks);
    	System.out.println();
    	
    	//Test DeleteTask method
    	String deleteTaskResult = testClient.DeleteTask(testTaskId);
    	System.out.println("deleteTaskResult: " + deleteTaskResult);
    	System.out.println();
    	
    	//Test get GetAttendantTasks method to verify test calls to CreateTask and DeleteTask
    	attendantsTasks =  testClient.GetAttendantTasks(testAttendant);
     	//Attendants task as xml
    	System.out.println(testAttendant + "'s  tasks: \n" + attendantsTasks);
    	System.out.println();
    	
    	//Test on testAttendant2
    	attendantsTasks =  testClient.GetAttendantTasks(testAttendant2);
     	//Attendants task as xml
    	System.out.println(testAttendant2 + "'s  tasks: \n" + attendantsTasks);
    	System.out.println();
    	
    	
    }
}
